package cn.kpi.service.usualassessment.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.kpi.dao.usualassessment.UserUATPjDao;
import cn.kpi.entity.usualassessment.UserUATHpDetailEntity;
import cn.kpi.entity.usualassessment.UserUATLdDetailEntity;
import cn.kpi.entity.usualassessment.UserUATPjSummaryEntity;
import cn.kpi.service.usualassessment.IUserUATPjService;

@Service
public class UserUATPjServiceImpl implements IUserUATPjService {

	@Autowired
	private UserUATPjDao userUATPjDao;
	
	
//	@Transactional
//	public int InsertUserUATPj(UserUATPjSummaryEntity entity,Long userid,Long templateid,Map<Long,Integer> map)throws Exception{
//		try{
//			int result=userUATPjDao.InsertUserUATPjSummary(entity);
//			Long usasspjid=entity.getUsasspjid();
//			int icount=0;
//			if(entity.getPjtype()==0){//互评
//				result+=userUATPjDao.InsertUserUATHpDetail(usasspjid, userid, templateid);
//				for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//					  result+=userUATPjDao.UpdateUserUATHpDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
//					  icount++;
//				}
//			}
//			else{
//				result+=userUATPjDao.InsertUserUATLdDetail(usasspjid, userid, templateid);
//				for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//					  result+=userUATPjDao.UpdateUserUATLdDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
//					  icount++;
//				}
//			}
//			
//			System.out.println(result+" "+icount);
//			if(result<=(icount+1)){
//				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
//				
//				return -1;
//			}
//			return result;
//		}
//		catch(Exception ex){
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
//			ex.printStackTrace();
//			return -1;
//		}
//	}
//	
//	@Transactional
//	public int UpdateUserUATPj(UserUATPjSummaryEntity entity,Long userid,Long templateid,Map<Long,Integer> map)throws Exception{
//		try{
//			int result=userUATPjDao.UpdateUserUATPjSummary(entity);
//			Long usasspjid=entity.getUsasspjid();
//			if(entity.getPjtype()==0){//互评
//				for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//					  result+=userUATPjDao.UpdateUserUATHpDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
//				}
//			}
//			else{
//				for (Map.Entry<Long,Integer> entry : map.entrySet()) { 
//					  result+=userUATPjDao.UpdateUserUATLdDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
//				}
//			}
//			return result;
//		}
//		catch(Exception ex){
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
//			ex.printStackTrace();
//			return -1;
//		}
//	}
	
	
	@Transactional
	public int InsertUserUATPj(UserUATPjSummaryEntity entity,Long userid,Long templateid,Map<Long,Float> map,int audioid,String audioname)throws Exception{
		try{
			int result=userUATPjDao.InsertUserUATPjSummary(entity);
			Long usasspjid=entity.getUsasspjid();
			int icount=0;
			if(entity.getPjtype()==0){//互评
				result+=userUATPjDao.InsertUserUATHpDetail(usasspjid, userid, templateid);
				for (Map.Entry<Long,Float> entry : map.entrySet()) { 
					  result+=userUATPjDao.UpdateUserUATHpDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
					  icount++;
				}
			}
			else{
				result+=userUATPjDao.InsertUserUATLdDetail(usasspjid, userid, templateid);
				for (Map.Entry<Long,Float> entry : map.entrySet()) { 
					  result+=userUATPjDao.UpdateUserUATLdDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue(),audioid,audioname);
					  icount++;
				}
			}
			
			System.out.println(result+" "+icount);
			if(result<=(icount+1)){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物 
				
				return -1;
			}
			return result;
		}
		catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
			ex.printStackTrace();
			return -1;
		}
	}
	
	@Transactional
	public int UpdateUserUATPj(UserUATPjSummaryEntity entity,Long userid,Long templateid,Map<Long,Float> map,int audioid,String audioname)throws Exception{
		try{
			int result=userUATPjDao.UpdateUserUATPjSummary(entity);
			Long usasspjid=entity.getUsasspjid();
			if(entity.getPjtype()==0){//互评
				for (Map.Entry<Long,Float> entry : map.entrySet()) { 
					  result+=userUATPjDao.UpdateUserUATHpDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue());
				}
			}
			else{
				for (Map.Entry<Long,Float> entry : map.entrySet()) { 
					  result+=userUATPjDao.UpdateUserUATLdDetailScore(usasspjid, userid, templateid, entry.getKey(), entry.getValue(),audioid,audioname);
				}
			}
			return result;
		}
		catch(Exception ex){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//回滚事物
			ex.printStackTrace();
			return -1;
		}
	}
	
	public List<UserUATHpDetailEntity> GetUserUATHpDetailList(Long usasspjid,Long userid)throws Exception{
		try{
			return userUATPjDao.GetUserUATHpDetailList(usasspjid, userid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<UserUATLdDetailEntity> GetUserUATLdDetailList(Long usasspjid,Long userid)throws Exception{
		try{
			return userUATPjDao.GetUserUATLdDetailList(usasspjid, userid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public UserUATPjSummaryEntity GetUserUATPjSummary(Long userid,Long templateid,int year,int quarter,int pjtype)throws Exception{
		try{
			return userUATPjDao.GetUserUATPjSummary(userid, templateid, year, quarter,pjtype);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public List<UserUATPjSummaryEntity> GetUserUATPjSummaryList(int pjtype,String where)throws Exception{
		try{
			return userUATPjDao.GetUserUATPjSummaryList(pjtype,where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public int GetUserUATPjSummaryListCount(int pjtype,String where)throws Exception{
		try{
			return userUATPjDao.GetUserUATPjSummaryListCount(pjtype,where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	
	public int DeleteUATHp(Long usasspjid,Long userid)throws Exception{
		try{
			return userUATPjDao.DeleteUATHp(usasspjid,userid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	public int DeleteUATLD(Long usasspjid,Long userid)throws Exception{
		try{
			return userUATPjDao.DeleteUATLD(usasspjid,userid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}
	public UserUATPjSummaryEntity GetUserUATPjSummaryForMzpd(Long userid,int year,int quarter,int pjtype)throws Exception{
		try{
			return userUATPjDao.GetUserUATPjSummaryForMzpd(userid,year,quarter,pjtype);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<UserUATPjSummaryEntity> GetUserPjListProcess(String where,int year,int quarter,int pjtype,int beginrow,int endrow)throws Exception{
		try{
			return userUATPjDao.GetUserPjListProcess(where, year, quarter, pjtype,beginrow,endrow);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public List<UserUATPjSummaryEntity> GetOldUserPjListProcess(String where, int year, int quarter, int pjtype, int beginrow, int endrow) throws Exception {
		try{
			return userUATPjDao.GetOldUserPjListProcess(where, year, quarter, pjtype,beginrow,endrow);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}

	public int GetUserPjListProcessCount(String where)throws Exception{
		try{
			return userUATPjDao.GetUserPjListProcessCount(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

	@Override
	public int GetOldUserPjListProcessCount(String where) throws Exception {
		try{
			return userUATPjDao.GetOldUserPjListProcessCount(where);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return -1;
		}
	}

	public List<Map> GetPjOrgSumList(String where,int year,int quarter)throws Exception{
		try{
			return userUATPjDao.GetPjOrgSumList(where,year,quarter);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<Map> GetPjOrgSumListByStatustype(String where,int year,int quarter,String pjtype)throws Exception{
		try{
			return userUATPjDao.GetPjOrgSumListByStatustype(where,year,quarter,pjtype);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public List<UserUATHpDetailEntity> GetUserUATHpDetailListRowSpan(Long usasspjid)throws Exception{
		try{
			return userUATPjDao.GetUserUATHpDetailListRowSpan(usasspjid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	public List<UserUATLdDetailEntity> GetUserUATLdDetailListRowSpan(Long usasspjid)throws Exception{
		try{
			return userUATPjDao.GetUserUATLdDetailListRowSpan(usasspjid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public UserUATPjSummaryEntity GetUserUATPjSummaryById(Long usasspjid)throws Exception{
		try{
			return userUATPjDao.GetUserUATPjSummaryById(usasspjid);
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	@Transactional
	public int UpdateUATHpStatus(Long usasspjid,Long userid,int status,int year,int quarter)throws Exception{
		try{
			int result=0;
			result+=userUATPjDao.UpdateStatus(usasspjid, status);
			result+=userUATPjDao.UpdateUsasstempNodeHp(usasspjid, userid, year, quarter);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	@Transactional
	public int UpdateUATLdStatus(Long usasspjid,Long userid,int status,int year,int quarter)throws Exception{
		try{
			int result=0;
			result+=userUATPjDao.UpdateStatus(usasspjid, status);
			result+=userUATPjDao.UpdateUsasstempNodeLd(usasspjid, userid, year, quarter);
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	}
	
	@Transactional
	 public int UpdateUATHpStatusBmNew(Long orgid,int status,int year,int quarter,Long orgstatustjid,String statustype)throws Exception{
		try{
			int result=0;
			if(orgstatustjid==0){
				result+=userUATPjDao.InsertOrgStatusEntity(orgid,year,quarter,statustype);
			}
			else{
				result+=userUATPjDao.UpdateOrgStatusEntityTj(orgstatustjid);
			}
			if(statustype.equals("hp")){
				result+=userUATPjDao.UpdateUsasstempNodeHpBm(orgid, year,quarter,status,0);
				result+=userUATPjDao.UpdatePjStatusBm(orgid, year,quarter,status,0);
				
			}
			else{
				result+=userUATPjDao.UpdateUsasstempNodeHpBm(orgid, year,quarter,status,1);
				result+=userUATPjDao.UpdatePjStatusBm(orgid, year,quarter,status,1);
			}
			
			
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	 }
	 public int GetOrgStatusCount(Long orgid,int year,int quarter,String statustype)throws Exception{
		 try{
			 return userUATPjDao.GetOrgStatusCount(orgid,year,quarter,statustype);
		 }
		 catch(Exception ex){
			 ex.printStackTrace();
			 return -1;
		 }
	 }
	 
	 @Transactional
	 public int UpdateUATLdStatusBmNew(Long orgid,int status,int year,int quarter,Long orgstatustjid,String statustype)throws Exception{
		try{
			int result=0;
			if(orgstatustjid==0){
				result+=userUATPjDao.InsertOrgStatusEntity(orgid,year,quarter,statustype);
			}
			else{
				result+=userUATPjDao.UpdateOrgStatusEntityTj(orgstatustjid);
			}
			if(statustype.equals("hp")){
				result+=userUATPjDao.UpdateUsasstempNodeLdBm(orgid, year,quarter,status,0);
				result+=userUATPjDao.UpdatePjStatusBm(orgid, year,quarter,status,0);
				
			}
			else{
				result+=userUATPjDao.UpdateUsasstempNodeLdBm(orgid, year,quarter,status,1);
				result+=userUATPjDao.UpdatePjStatusBm(orgid, year,quarter,status,1);
			
			}
			
			
			return result;
		}
		catch(Exception ex){
			ex.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return -1;
		}
	 }
	
}
